<?php
namespace Admin\Controller;
use Think\Controller;
class AgentController extends Controller {
	//代理商列表
    public function theAgent(){

    	$comList=getPages(M('admin'));

    	foreach ($comList["comList"] as $key => $value) {
    		$comList["comList"][$key]['last_ip']=long2ip($value['last_ip']);
    	}

    	$this->assign("comList",$comList["comList"]);
        $this->assign("page",$comList["page"]);	
        
        $this->display();
    }

    //代理商添加
    public function agentAddition(){

    	$this->display();
    }
    //代理商修改
    public function agentChange(){
    	$adminMode=M('admin');
    	if(IS_POST){
    		$map=I('post.');
    		$data=array(
    			'username'=>$map['username'],
    			'rel_name'=>$map['rel_name'],
    			'tel'=>$map['tel'],
    			'is_apply'=>$map['is_apply'],
    			'last_ip'=>getUserIp(),
    			'balance'=>$map['balance'],
    			'proportion'=>$map['proportion'],
    			'add_time'=>date("Y-m-d H:i:s")
    			);
    		if($map['password']){
    				$data['password']=md5($map['password']);
    			}
    		if($map['id']){
    			$data['id']=$map['id'];
    			

    			$res=$adminMode->save($data);
    		}else{
    			$res=$adminMode->add($data);
    		}

    		if($res){
    			$this->success("操作成功","");
    		}else{
    			$this->error("操作失败","");
    		}

    	}else{
    		$id=I('get.id');
    		if(!$id){
    			$this->error("未获取到信息id","");
    		}
    		$adminInfo=$adminMode->where('id='.$id)->find();
    		$this->assign("adminInfo",$adminInfo);


    		$this->display();	
    	}


    	
    }
    //收支明细
    public function paymentDetails(){

    	$this->display();
    }
    //奖励规则
    public function rewardRules(){
    	if(IS_POST){
    		$map=I('post.');
    		$ids=implode(",", $map['id']);
    		if(!$ids){
    			$this->error("未获取到信息id");
    		}
    		$arr=array_combine($map['id'],$map['divided_into']);
    		$sql="UPDATE cloud_model set divided_into = CASE id ";
    		foreach ($arr as $key => $value) {
    			$sql .= sprintf("WHEN %d THEN %d ",$key, $value);
    		}
    		$sql .= "END WHERE id IN (".$ids.")";



    		$res=M()->execute($sql);
    		if($res){
    			$this->success("操作成功","");
    		}else{
    			$this->error("操作失败","");
    		}
    	}else{
	 		$modelList=M('model')->select();

	    	$this->assign("modelList",$modelList);
	    	$this->display();   		
    	}
    	
    }
    
}